package com.examination.mlib.utils;

import android.Manifest;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.pdf.PdfRenderer;
import android.os.Build;
import android.os.Environment;
import android.os.ParcelFileDescriptor;
import android.provider.MediaStore;

import androidx.annotation.RequiresApi;

import com.examination.mlib.interfaceapi.NetWorkCallBack;
import com.examination.mlib.manager.PermissionManager;
import com.yilijk.base.AppBase;
import com.yilijk.base.network.https.HttpCallBack;
import com.yilijk.base.network.https.HttpUtils;
import com.yilijk.base.network.https.interceptor.NetWorkUtils;
import com.yilijk.base.utils.AppUtils;

import java.io.File;
import java.io.IOException;

/**
 * Created by SHICHENG
 * <p>
 * Time on 2021/12/20
 */
public class PDFUtils {

    private static PDFUtils pdfUtils;

    public static synchronized  PDFUtils getPDFUtils(){
        if (pdfUtils==null){
            pdfUtils = new PDFUtils();
        }
        return pdfUtils;
    }


    public void pdf2img(Activity activity,String url,HttpCallBack netWorkCallBack){
        PermissionManager.getInstance()
                .get(activity)
                .requestPermissions(
                        new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.READ_EXTERNAL_STORAGE}
                        ,
                        new String[]{"允许应用使用文件存储权限","允许应用使用文件读取权限"})
                .requestCodes(0xa1)
                .setmRequestPermissionCallBack(new PermissionManager.RequestPermissionCallBack() {
                    @Override
                    public void noM() {

                    }

                    @Override
                    public void granted() {
                        downloadPdf(activity,url,netWorkCallBack);
                    }

                    @Override
                    public void denied() {

                    }
                }).request();
    }


    ParcelFileDescriptor parcelFileDescriptor;
    PdfRenderer pdfRenderer;
    PdfRenderer.Page page;

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public void PDF2BitMap(String filepath) throws IOException {
        File file = new File(filepath);
        parcelFileDescriptor = ParcelFileDescriptor.open(file,ParcelFileDescriptor.MODE_READ_ONLY);
//        ParcelFileDescriptor parcelFileDescriptor = getAssets().openFd("").getParcelFileDescriptor();
        pdfRenderer = new PdfRenderer(parcelFileDescriptor);
        //多页数PDF
//        int pageCount = pdfRenderer.getPageCount();
//        for (int i = 0; i < pageCount; i++) {
//            PdfRenderer.Page page = pdfRenderer.openPage(i);
//            Bitmap bitmap = Bitmap.createBitmap(page.getWidth(), page.getHeight(), Bitmap.Config.ARGB_8888);
//            page.render(bitmap,null,null,PdfRenderer.Page.RENDER_MODE_FOR_PRINT);
//            MediaStore.Images.Media.insertImage(AppUtils.getApplication().getContentResolver(), bitmap,  "发票"+System.currentTimeMillis(), "暂无");
//        }
        page = pdfRenderer.openPage(0);
        Bitmap bitmap = Bitmap.createBitmap(page.getWidth(), page.getHeight(), Bitmap.Config.ARGB_8888);
        page.render(bitmap,null,null,PdfRenderer.Page.RENDER_MODE_FOR_PRINT);
        MediaStore.Images.Media.insertImage(AppBase.getApplication().getContentResolver(), bitmap,  "发票"+System.currentTimeMillis(), "暂无");

        clearPDFRes();
        file.delete();
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    void clearPDFRes() throws IOException {
        if (parcelFileDescriptor!=null){
            parcelFileDescriptor.close();
            parcelFileDescriptor = null;
        }
        if (page !=null){
            page.close();
            page = null;
        }
        if (pdfRenderer!=null){
            pdfRenderer.close();
            pdfRenderer = null;
        }
    }
    ///data/data/com.ylkj.patient/files
    private String savePDFfilePath = Environment.getExternalStorageDirectory().getPath()+"/Download/";
    private String savePDFfileName = "ticket.pdf";
    void downloadPdf(Activity activity, String pdfurl, HttpCallBack httpCallBack){
        HttpUtils.downloadFile(savePDFfilePath,savePDFfileName,pdfurl,httpCallBack);
    }

}
